package work;

import work.config.Constant;
import work.fram.JDBCConnect;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class IdCardFormat {

    private static File idCardHandle = new File(Constant.work, "身份证号处理后.txt");
    private static File idCardCannotHandle = new File(Constant.work, "身份证处理后仍不符合要求.txt");

    private static Writer idCardW;
    private static Writer cannotIdCardW;

    static {
        if (!idCardHandle.exists()) {
            try {
                idCardHandle.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (!idCardHandle.exists()) {
            try {
                idCardHandle.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        try {
            idCardW = new FileWriter(idCardHandle);
            cannotIdCardW = new FileWriter(idCardCannotHandle);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    public static void main(String[] args) throws SQLException, IOException {


        String regex = "^[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";

        //System.out.println("37292519950x".matches(regex));
        Statement statement = JDBCConnect.getStatement();

        ResultSet resultSet = statement.executeQuery("SELECT sysid,idcard FROM tc_md_personnel ");

        while (resultSet.next()) {

            String id = resultSet.getString("sysid");
            String idcard = resultSet.getString("idcard");

            if (idcard != null) {
                if (!idcard.matches(regex)) {

                    String format = work.utils.IdCardFormat.format(idcard);

                    if (format.matches(regex)) {

                        String sql = "UPDATE tc_md_personnel SET idcard=\"" + format + "\" WHERE id=\"" + id + "\";";

                        idCardW.write(sql + "\n");
                    } else {
                        cannotIdCardW.write(id + ":" + idcard + "\n");
                    }

                }
            }


        }

        idCardW.flush();

        idCardW.close();

        cannotIdCardW.flush();

        cannotIdCardW.close();

    }

}
